#ifndef MESSAGE_H
#define MESSAGE_H

#include <QByteArray>
#include <QString>
#include "file_info.h"
#include "remote_tree_model.h"
#include "common.h"

enum MessageType {
	FILE_INFO_REQ, FILE_CONT_REQ, TREE_MODEL_REQ, DISCONNECT, // Used by client
	FILE_INFO, FILE_CONT, TREE_MODEL, REQ_REJECT // Used by server
};

class Message {
public:
	static QByteArray encode_file_info_req_msg (const QString &rel_path);
	static QByteArray encode_file_cont_req_msg (const QString &rel_path);
	static QByteArray encode_tree_model_req_msg ();
	static QByteArray encode_tree_model_msg (const RemoteTreeModel &rtm);
	static QByteArray encode_disconnect_msg ();

	static QByteArray encode_file_info_msg (const FileInfo &file_info);
	static QByteArray encode_file_cont_msg (const QByteArray &file_cont);
	static QByteArray encode_req_reject_msg ();

	static MessageType encoded_msg_type (const QByteArray &encoded);
	static bool check_msg_length (const QByteArray &encoded);
	static QByteArray encoded_msg_body (const QByteArray &encoded);

	static FileInfo decode_file_info_msg (const QByteArray &encoded);
	static RemoteTreeModel* decode_tree_model_msg (const QByteArray &encoded);

private:
	Message (MessageType msg_type);

	// Helper functions
	static QByteArray encode_msg_base (MessageType msg_type, uint length);
	static QByteArray encode_msg_field (const QByteArray &data);
	static QList<QByteArray> decode_msg_fields (const QByteArray &data);
};

void message_test0 ();

#endif // MESSAGE_H
